package com.judian.smart.util;

import android.annotation.SuppressLint;
import android.content.Context;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * author: Peter
 * date: 2019/10/11 星期五 12:04
 * desc: 时间工具类
 */
public class TimeUtil {
    public Context context;
    public Date date;

    public TimeUtil(Context context) {
        this.context = context;
        date = new Date();
    }

    //获取当前时间
    public int[] Date_Func_Array() {
        int[] Date_Array = new int[6];
        Date_Array[0] = Integer.valueOf(_GetYear());
        Date_Array[1] = Integer.valueOf(_GetMonth());
        Date_Array[2] = Integer.valueOf(_GetDays());
        Date_Array[3] = Integer.valueOf(_GetHour());
        Date_Array[4] = Integer.valueOf(_GetMinute());
        Date_Array[5] = Integer.valueOf(_GetSecond());
        return Date_Array;
    }

    //获取当前日期
    public String getTodaysDate() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sDateFormat.format(new Date());
    }

    //获取系统日期
    private String _GetYear() {
        String YEAR = "yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat(YEAR, Locale.getDefault());
        return sdf.format(date);
    }

    private String _GetMonth() {
        String MONTH = "MM";
        SimpleDateFormat sdf = new SimpleDateFormat(MONTH, Locale.getDefault());
        return sdf.format(date);
    }

    private String _GetHour() {
        String HOUR = "HH";
        SimpleDateFormat sdf = new SimpleDateFormat(HOUR, Locale.getDefault());
        return sdf.format(date);
    }

    private String _GetMinute() {
        String MINUTE = "mm";
        SimpleDateFormat sdf = new SimpleDateFormat(MINUTE, Locale.getDefault());
        return sdf.format(date);
    }

    private String _GetSecond() {
        String SECOND = "ss";
        SimpleDateFormat sdf = new SimpleDateFormat(SECOND, Locale.getDefault());
        return sdf.format(date);
    }

    private String _GetDays() {
        String DAY = "dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DAY, Locale.getDefault());
        return sdf.format(date);
    }

    //获取周几
    @SuppressLint("SimpleDateFormat")
    public String _GetWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat("E ");
        return sdf.format(date);
    }

    //获取星期几
    public String getWeek() {
        String mWay;
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        switch (mWay) {
            case "1":
                mWay = "天";
                break;
            case "2":
                mWay = "一";
                break;
            case "3":
                mWay = "二";
                break;
            case "4":
                mWay = "三";
                break;
            case "5":
                mWay = "四";
                break;
            case "6":
                mWay = "五";
                break;
            case "7":
                mWay = "六";
                break;
        }
        return "星期" + mWay;
    }

    public String getWeek(Date curDate) {
        String mWay;
        final Calendar c = Calendar.getInstance();
        c.setTime(curDate);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        switch (mWay) {
            case "1":
                mWay = "天";
                break;
            case "2":
                mWay = "一";
                break;
            case "3":
                mWay = "二";
                break;
            case "4":
                mWay = "三";
                break;
            case "5":
                mWay = "四";
                break;
            case "6":
                mWay = "五";
                break;
            case "7":
                mWay = "六";
                break;
        }
        return "星期" + mWay;
    }


    //获取明天的日期
    public Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, +1);//+1今天的时间加一天
        date = calendar.getTime();
        return date;
    }

    @SuppressLint("SimpleDateFormat")
    public String[] _GetTomorrowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//24小时制
        String TomorrowDate = sdf.format(getNextDay(new Date()));
        String[] TomorrowDateArray = new String[3];
        TomorrowDateArray = TomorrowDate.split("-");
        int[] iTomorrowDateArray = new int[3];
        for (int i = 2; i >= 0; i--) {
            iTomorrowDateArray[i] = Integer.valueOf(TomorrowDateArray[i]);
        }
        return TomorrowDateArray;
    }

    //获取上下午
    public int get_AMorPM() {
        long time = System.currentTimeMillis();
        final Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(time);
        int apm = mCalendar.get(Calendar.AM_PM);
        return apm;
    }

    /**
     * 获取当前日期是星期几
     *
     * @return 当前日期是星期几
     */
    public int getWeekOfDate() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return w;
    }

    //日期排序
    public String[] getsortWeekOfDate() {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        String[] sortWeekDays = new String[7];
        for (int i = 0; i < 7; i++) {
            sortWeekDays[i] = weekDays[(getWeekOfDate() + i) % 7];
        }
        return sortWeekDays;
    }
}
